package com.auxgroup.hr.front.site.co.modPerson;

import javax.annotation.Resource;

import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCModPerPrimary;
import com.auxgroup.hr.front.dao.domain.PsCModTrain;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.modperson.ModTrainService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;

/**
 * 培训经历流程信息CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/modTrain")
public class ModTrainCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(ModTrainCO.class);

	@Autowired
	public ModTrainService modTrainService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 培训经历的增删改
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(PsCModTrain modTrain,@SessionGain("user") UserDTO user) {
		logger.info("saveModTrain param= {}", modTrain);
		if(StringUtils.isBlank(modTrain.getcTransactionNbr())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "流程编号不能为空");
		}
		if(StringUtils.isBlank(modTrain.getEmplid())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "员工ID不能为空");
		}
		if(StringUtils.isBlank(modTrain.getcModStauts())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "数据状态不能为空");
		}
		if(modTrain.getcSeqno()!=null){
			PsCModPerPrimary primary = new PsCModPerPrimary();
			primary.setcTransactionNbr(modTrain.getcTransactionNbr());
			primary.setEmplid(modTrain.getEmplid());
			primary.setcSeqno(modTrain.getcSeqno());
			PsCModTrain nowTrain = modTrainService.getModTrain(primary);
			if(nowTrain!=null){
				objectMapper.map(modTrain, nowTrain,"notCopyNull_modTrain");
				modTrain = modTrainService.save(nowTrain,user);
			}else{
				modTrain = modTrainService.save(modTrain,user);
			}
		}else{
			modTrain = modTrainService.save(modTrain,user);
		}
		if(modTrain.getcSeqno()!=null){
			return buildRespone(true);
		}
		return buildRespone(false);
	}
	
	/**
	 * 获取培训经历流程信息
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(PsCModPerPrimary modPerPrimary) {
		logger.info("getModTrain param= {}", modPerPrimary);
		PsCModTrain modTrain = modTrainService.getModTrain(modPerPrimary);
		return buildRespone(modTrain);
	}
	
}
